package com.eos.inter.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.eos.common.tool.CoreKeyContext;
import com.eos.common.tool.DaoAccessObjectPoolContextFactory;
import com.eos.orm.mybatis.dao.CustGuaranteeMonthReportMapper;
import com.eos.orm.mybatis.dao.PerformAmountMonthReportMapper;
import com.eos.orm.mybatis.model.CustGuaranteeMonthReport;
import com.eos.orm.mybatis.model.CustGuaranteeMonthReportExample;
import com.eos.orm.mybatis.model.PerformAmountMonthReport;
import com.eos.orm.mybatis.model.PerformAmountMonthReportExample;
import com.eos.orm.mybatis.model.CustGuaranteeMonthReportExample.Criteria;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaYunosBiContractphoneAddRequest;
import com.taobao.api.response.AlibabaYunosBiContractphoneAddResponse;

/**
 * 上传数据给阿里接口
 * 
 * @author sz
 * 
 */
public class AlipyTaobaoServlet extends BaseHttpServlet {

	private static final long serialVersionUID = 1L;
	private CustGuaranteeMonthReportMapper mapper = (CustGuaranteeMonthReportMapper) DaoAccessObjectPoolContextFactory
			.getInstaAccessObjectPoolContext().instanceDataAccessObject(
					"CustGuaranteeMonthReportMapper");
	private PerformAmountMonthReportMapper per_mapper = (PerformAmountMonthReportMapper) DaoAccessObjectPoolContextFactory
			.getInstaAccessObjectPoolContext().instanceDataAccessObject(
					"PerformAmountMonthReportMapper");
	private static String appkey = CoreKeyContext.getCoreKey("alipy_appkey");
	private static String secret = CoreKeyContext.getCoreKey("alipy_secret");
	private static String url = CoreKeyContext.getCoreKey("alipy_url");

	public void submit_CustGuaranteeMonthReport(String appkey, String secret,
			String url) {
		TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
		AlibabaYunosBiContractphoneAddRequest req = new AlibabaYunosBiContractphoneAddRequest();
		CustGuaranteeMonthReportExample example = new CustGuaranteeMonthReportExample();
		Criteria criteria = example.createCriteria();
		criteria.andStatusEqualTo("0");
		List<CustGuaranteeMonthReport> templist = mapper
				.selectByExample(example);
		req.setTableName("cust_guarantee_month_report");
		try {
			for (CustGuaranteeMonthReport temp : templist) {
				req.setContent("partner_name=" + temp.getPartnerName()
						+ ",cust_name=" + temp.getCustName() + ",cust_id_card="
						+ temp.getCustIdCard() + ",taobao_order_id="
						+ temp.getTaobaoOrderId() + ",guarantee_mode="
						+ temp.getGuaranteeModel() + ",equipment_amount="
						+ temp.getEquipmentAmount() + ",package_amount="
						+ temp.getPackageAmount() + ",guarantee_fee="
						+ temp.getGuaranteeFee() + ",report_month="
						+ temp.getReportMonth());
				AlibabaYunosBiContractphoneAddResponse response = client
						.execute(req);
				if (null != response && "0".equals(response.getStatus())) {
					temp.setStatus("1");
					mapper.updateByPrimaryKeySelective(temp);
				}
			}
		} catch (ApiException e) {
			System.out
					.println("com.eos.inter.servlet.AlipyTaobaoServlet.submit_CustGuaranteeMonthReport throws exception:"
							+ e.getMessage());
		} finally {
			templist = null;
			req = null;
			client = null;
		}
	}

	public void submit_PerformAmountMonthReport(String appkey, String secret,
			String url) {
		TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
		AlibabaYunosBiContractphoneAddRequest req = new AlibabaYunosBiContractphoneAddRequest();
		PerformAmountMonthReportExample example = new PerformAmountMonthReportExample();
		com.eos.orm.mybatis.model.PerformAmountMonthReportExample.Criteria critera = example
				.createCriteria();
		critera.andStatusEqualTo("0");
		List<PerformAmountMonthReport> templist = per_mapper
				.selectByExample(example);
		req.setTableName("perform_amount_month_report");
		try {
			for (PerformAmountMonthReport temp : templist) {
				req.setContent("partner_name=" + temp.getPartnerName()
						+ ",cust_name=" + temp.getCustName() + ",cust_id_card="
						+ temp.getCustIdCard() + ",taobao_order_id="
						+ temp.getTaobaoOrderId() + ",cust_contract_phone="
						+ temp.getCustContractPhone() + ",package_type="
						+ temp.getPackageType() + ",package_total_amount="
						+ temp.getPackageTotalAmount() + ",delay_total_amount="
						+ temp.getDelayTotalAmount()
						+ ",operators_perform_amount="
						+ temp.getOperatorsPerformAmount()
						+ ",yidao_perform_amount="
						+ temp.getYidaoPerformAmount() + ",report_month="
						+ temp.getReportMonth());
				AlibabaYunosBiContractphoneAddResponse response = client
						.execute(req);
				if (null != response && "0".equals(response.getStatus())) {
					temp.setStatus("1");
					per_mapper.updateByPrimaryKeySelective(temp);
				}
			}
		} catch (ApiException e) {
			System.out
					.println("com.eos.inter.servlet.AlipyTaobaoServlet.submit_PerformAmountMonthReport throws exception:"
							+ e.getMessage());
		} finally {
			templist = null;
			req = null;
			client = null;
		}
	}

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		submit_CustGuaranteeMonthReport(appkey, secret, url);
		submit_PerformAmountMonthReport(appkey, secret, url);
	}
}
